package cn.linkai.star.graph.core.service.impl;

import cn.linkai.star.graph.comfyui.client.pojo.ComfyuiTask;
import cn.linkai.star.graph.core.mapper.UserResultMapper;
import cn.linkai.star.graph.core.pojo.UserResult;
import cn.linkai.star.graph.core.service.UserFundRecordService;
import cn.linkai.star.graph.core.service.UserResultService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class UserResultServiceImpl extends ServiceImpl<UserResultMapper, UserResult> implements UserResultService {

    @Autowired
    private UserFundRecordService FundRecordService;

    @Override
    @Transactional
    public void insertUserResult(ComfyuiTask task, List<String> urls) {
        List<UserResult> userResults = urls.stream ().map (url -> {
            UserResult userResult = new UserResult ();
            userResult.setCreatedTime (LocalDateTime.now ());
            userResult.setCollect (0);
            userResult.setUrl (url);
            //用户id
            userResult.setUserId (task.getUserId ());
            return userResult;
        }).toList ();
        saveBatch (userResults);

        //积分划扣
        FundRecordService.pointsDeduction (task.getUserId (),task.getSize ());
    }
}
